################################################
#Code to reproduce Figure 1 [Value Coefficients]
################################################

library(rio)
library(tidyverse)
library(ggstance)

#####################
#Functions
####################

`%notin%` <- Negate(`%in%`)

m1_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 1")
}

m2_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 2")
}

m3_func <- function(x) {
  x %>%
    filter(parm %notin% c("_cons", "1.post_soviet", "demyrs_01", "ethnic_01")) %>%
    filter(!is.na(z)) %>%
    mutate(model = "Model 3")
}

#####################
#Vote
####################

v1 <- import("vote_elections1.dta") %>% m1_func()
v2 <- import("vote_elections2.dta") %>% m2_func()
v3 <- import("vote_elections3.dta") %>% m3_func()

#####################
#Support Others
####################
s1 <- import("support_others1.dta") %>% m1_func()
s2 <- import("support_others2.dta") %>% m2_func()
s3 <- import("support_others3.dta") %>% m3_func()

#####################
#Obey
####################
o1 <- import("obey1.dta") %>% m1_func()
o2 <- import("obey2.dta") %>% m2_func()
o3 <- import("obey3.dta") %>% m3_func()

#####################
#Opinions
####################
op1 <- import("opinions1.dta") %>% m1_func()
op2 <- import("opinions2.dta") %>% m2_func()
op3 <- import("opinions3.dta") %>% m3_func()

#####################
#Volunteer
####################

vo1 <- import("volunteer1.dta") %>% m1_func()
vo2 <- import("volunteer2.dta") %>% m2_func()
vo3 <- import("volunteer3.dta") %>% m3_func()

#####################
#Be Active in Politics
####################

a1 <- import("beactive1.dta") %>% m1_func()
a2 <- import("beactive2.dta") %>% m2_func()
a3 <- import("beactive3.dta") %>% m3_func()

#####################
#Combine (Export)
####################

comb <- bind_rows(v1, v2, v3, 
                  a1, a2, a3, 
                  o1, o2, o3, 
                  op1, op2, op3, 
                  s1, s2, s3,
                  vo1, vo2, vo3)

export(comb, "maintext_combined.csv")

#####################
#Figure
####################

comb1 <- comb %>%
  filter(parm %in% c("self_te", "cons_open"))

comb1a <- comb1 %>%
  mutate(label = factor(label, 
                        levels=c("Self Transcendence vs. Self-Enhancement", 
                                 "Conservation vs. Openness")), 
         model = factor(model, 
                        levels=c("Model 3", "Model 2", "Model 1")), 
         eq = factor(eq, 
                     levels=c("vote_elections", "beactive", "volunteer", 
                              "obey", "opinions", "support_others"), 
                     labels=c("Vote", "Be Active", "Volunteer", 
                              "Obey", "Opinions", "Support Others")))

#With labels
comb1a %>%
  ggplot(aes(x=estimate, y=model)) + 
  geom_pointrangeh(aes(xmin=min95, xmax=max95)) + 
  facet_grid(eq ~ label, labeller = label_wrap_gen(width = 25)) + 
  geom_vline(xintercept=0, linetype="dashed", color="red") + 
  labs(y=NULL, x = "Coefficient") + 
  geom_text(aes(label=round(estimate, 2)), 
            nudge_y=0.3) +
  theme_bw(14) + 
  theme(strip.text.y = element_text(angle=0), 
        axis.title.y = element_text(margin = margin(r=15)), 
        strip.text = element_text(face = "bold"))

ggsave("figure1_labels.png", height=10, width=12, dpi=600)



